unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, BaseGrid, AdvGrid, StdCtrls;

type
  TForm1 = class(TForm)
    GridA: TAdvStringGrid;
    GridB: TAdvStringGrid;
    Button1: TButton;
    Button2: TButton;
    StringGrid1: TStringGrid;
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
  public
    procedure SetGridParams;
  end;

  TColObj=class(TObject)
  public
    Idex:Integer;
    Code:string;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  StrsColA:TStringList;
  StrsColB:TStringList;
  I,M,N   :Integer;
  StrA,StrB:string;
  Idex    :Integer;
begin
  StrsColA:=TStringList.Create;
  StrsColB:=TStringList.Create;

  for I := 0 to GridA.ColCount - 1 do
  begin
    StrsColA.Add(GridA.Cells[I,0]);
    StrsColB.Add(GridB.Cells[I,0]);
  end;


  GridB.BeginUpdate;
  N:=0;
  for M := 0 to StrsColA.Count - 1 do
  begin
    StrA:=StrsColA.Strings[M];
    Idex:=StrsColB.IndexOf(StrA);
    GridB.MoveColumn(Idex,M);

    StrsColB.Move(Idex,M);
  end;
  Writeln(Format('%D',[N]));
  GridB.EndUpdate;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  I:Integer;
begin
  //GridB.MoveColumn(19,8);
  for I := 0 to GridB.ColCount - 1 do
  begin
    GridB.MoveColumn(19,I);
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  WindowState:=wsMaximized;

end;

procedure TForm1.FormShow(Sender: TObject);
begin
  SetGridParams;
end;

procedure TForm1.SetGridParams;
var
  I     :Integer;
  ColObj:TColObj;
begin
  with GridA,GridB do
  begin
    GridA.ColCount:=20;
    GridB.ColCount:=20;

    GridA.Align:=alTop;
    GridB.Align:=alTop;

    //GridA.Options:=GridA.Options+[goColMoving];
    GridB.Options:=GridB.Options+[goColMoving];

    for I := 0 to GridA.ColCount - 1 do
    begin
      ColObj:=TColObj.Create;
      ColObj.Idex:=0;
      ColObj.Code:=IntToStr(I);

      GridA.Cells[I,0]  :=ColObj.Code;
      GridA.Objects[I,0]:=ColObj;


      GridB.Cells[I,0]  :=ColObj.Code;
      GridB.Objects[I,0]:=ColObj;
    end;
  end;
end;

end.
